log using figures_main_pol, replace
cd  "\\micro.intra\Projekt\P0624$\P0624_Gem\Kitschelt\submission"
use main_data_pol, clear
*Figure 5
preserve
drop count
gen count=1
keep if main==1
foreach var in x  y {
	gen gr_`var'_50=(int(25*				(`var'+0.0000001))-0.5)/25 if `var'<0
	replace gr_`var'_50=(int(25*(`var'-0.0000001))+0.5)/25  if `var'>=0
}
keep if gr_y_50!=. & gr_x_50!=.
collapse(sum) count, by(gr_y_50 gr_x_50 parti_initial)
fillin gr_y gr_x parti_initial
bysort parti_initial: egen parti_count=sum(count)
gen share= count/parti_count
replace share=0 if share==.

twoway (scatter gr_y gr_x [fweight = share] if parti_initial=="B" & share!=0, mcolor(gold) mfcolor(none) msize(vtiny) msymbol(circle)) ,ylabel(, angle(horizontal))  scheme(s1color) xline(0, lpattern(dash) lcolor(black)) yline(0, lpattern(dash) lcolor(black))  title(Sweden Democrats) ytitle( Authoritarian-Libertarian) xtitle(Left-Right) legend(off) ysize(5) xsize(5.5)
graph save lab_circle_B, replace

twoway (scatter gr_y gr_x [fweight = share] if parti_initial=="G" & share!=0, mcolor(forest_green) mfcolor(none) msize(vtiny) msymbol(circle)) ,ylabel(, angle(horizontal))  scheme(s1color) xline(0, lpattern(dash) lcolor(black)) yline(0, lpattern(dash) lcolor(black))  title(Green Party) ytitle( Authoritarian-Libertarian) xtitle(Left-Right) legend(off) ysize(5) xsize(5.5)
graph save lab_circle_G, replace

twoway (scatter gr_y gr_x [fweight = share] if parti_initial=="M" & share!=0, mcolor(navy) mfcolor(none) msize(vtiny) msymbol(circle)) , ylabel(, angle(horizontal))   scheme(s1color) xline(0, lpattern(dash) lcolor(black)) yline(0, lpattern(dash) lcolor(black))  title(Conservatives) ytitle( Authoritarian-Libertarian) xtitle(Left-Right) legend(off) ysize(5) xsize(5.5)
graph save lab_circle_M, replace

twoway (scatter gr_y gr_x [fweight = share] if parti_initial=="S" & share!=0, mcolor(cranberry) mfcolor(none) msize(vtiny) msymbol(circle)) , ylabel(, angle(horizontal)) scheme(s1color) xline(0, lpattern(dash) lcolor(black)) yline(0, lpattern(dash) lcolor(black))  title(Social Democrats) ytitle( Authoritarian-Libertarian) xtitle(Left-Right) legend(off) ysize(5) xsize(5.5)
graph save lab_circle_S, replace
restore

graph combine  lab_circle_G.gph  lab_circle_B.gph  lab_circle_S.gph  lab_circle_M.gph ///
, col(2) row(2) scheme(s1mono) ycommon xcommon ///
 ysize(18) xsize(20) iscale(.7)
graph save Figure_5, replace
  
****Figure 6
preserve
keep if main==1
gen level =1 if nom==1 & vald!=1 
replace level =2 if vald==1 
replace level =3 if  nom_ri==1
replace level =4 if  vald_ri==1
drop if level==2002
keep party_ineng year x y level
collapse  (mean) y x  ,by(party_ineng level)
replace  party_ineng="C" if party_ineng=="Cons"

foreach var in y x{
	twoway (connected `var' level if party_ineng=="S", sort msize(zero) ///
 mlabel(party_ineng) mlabsize(medium) mlabcolor(maroon) ///
 mlabposition(3) lcolor(maroon) lwidth(thin) lpattern(vshortdash)) ///
 (connected `var' level if party_ineng=="C", sort msize(zero) ///
 mlabel(party_ineng) mlabsize(medium) mlabcolor(navy) ///
 mlabposition(3) lcolor(navy) lwidth(thin) lpattern(vshortdash)) ///
  (connected `var' level if party_ineng=="G" , sort msize(zero) ///
 mlabel(party_ineng) mlabsize(medium) mlabcolor(forest_green) ///
 mlabposition(9) lcolor(forest_green) lwidth(thin) lpattern(vshortdash)) ///
   (connected `var' level if party_ineng=="Sd" , sort msize(zero) ///
 mlabel(party_ineng) mlabsize(medium) mlabcolor(dkorange) ///
 mlabposition(9) lcolor(dkorange) lwidth(thin) lpattern(vshortdash)), ///
 xlabel(1 "Nominated Mun." 2 "Elected Mun." 3 "Nominated Parl" 4"Elected Parl", ) xtitle(Election Year) ytitle(Average Position) ///
scheme(s1mono)  ylabel(, angle(horizontal) grid)  title (`var')  xscale(range(.8 4.2)) ///
 legend(order(1 "Social Democrats" 2 "Conservatives" 3 "Green Party" 4 "Sweden Democrats") cols(2))
graph save   `var'_party_level, replace
}
graph combine  y_party_level.gph x_party_level.gph, scheme(s1mono) col(2) ysize(6) xsize(20) iscale(1.3) 
graph save  Figure_6, replace
restore

**Figure W6
foreach samp  in nom_all nom_all_fn06{
	preserve
	keep if `samp'==1
	keep party_ineng valar x y 
	collapse  (mean) y x  ,by(party_ineng valar)
	replace  party_ineng="C" if party_ineng=="Cons"

	foreach var in y x{
		twoway (connected `var' valar if party_ineng=="S", sort msize(zero) ///
			mlabel(party_ineng) mlabsize(medium) mlabcolor(maroon) ///
			mlabposition(3) lcolor(maroon) lwidth(thin) lpattern(vshortdash)) ///
			(connected `var' valar if party_ineng=="C", sort msize(zero) ///
			mlabel(party_ineng) mlabsize(medium) mlabcolor(navy) ///
			mlabposition(3) lcolor(navy) lwidth(thin) lpattern(vshortdash)) ///
			(connected `var' valar if party_ineng=="G" , sort msize(zero) ///
			mlabel(party_ineng) mlabsize(medium) mlabcolor(forest_green) ///
			mlabposition(9) lcolor(forest_green) lwidth(thin) lpattern(vshortdash)) ///
			(connected `var' valar if party_ineng=="Sd" , sort msize(zero) ///
			mlabel(party_ineng) mlabsize(medium) mlabcolor(dkorange) ///
			mlabposition(9) lcolor(dkorange) lwidth(thin) lpattern(vshortdash)), ///
			xtitle(Election Period) ytitle(Average Position) ///
			scheme(s1mono)  ylabel(, angle(horizontal) grid)  title (`var') xlabel(2006(4)2014)  xscale(range(2005 2015)) ///
			legend(order(1 "Social Democrats" 2 "Conservatives" 3 "Green Party" 4 "Sweden Democrats") cols(2))
			graph save   `var'_party_`samp', replace
	}
restore
}

graph combine  y_party_nom_all.gph x_party_nom_all.gph ///
	y_party_nom_all_fn06.gph x_party_nom_all_fn06.gph ///
	, scheme(s1mono) col(2) ysize(17) xsize(20) iscale(.7)  
graph save Figure_W6
*Create Figure W8
foreach samp in sm gb  {
	foreach var in x y{
		binscatter dum_`samp' `var'    if samp_`samp'==1 , nbins(100)
		graph save pred_`samp'_`var', replace
		reg dum_`samp' `var'    if samp_`samp'==1, beta
	}
}
graph combine  pred_gb_y.gph pred_gb_x.gph ///
  pred_sm_y.gph pred_sm_x.gph ///
, scheme(s1mono) col(2) ysize(17) xsize(20) iscale(.7)
graph save Figure_W8
log close